package Service;

import Dao.SearchByPageDao;
import Table.Book;
import Table.PageBean;

import java.util.ArrayList;

public class SearchByPageService {
    public static PageBean<Book> SearchByPage(String currentPage1, String rows1, String bookname, String auther) {
        int currentPage = Integer.parseInt(currentPage1);
        int rows = Integer.parseInt(rows1);
        PageBean<Book> pageBean = new PageBean<Book>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setRows(rows);
        //查询总记录数
        int totalCount = SearchByPageDao.SearchTotalCount(bookname, auther);
        pageBean.setTotalCount(totalCount);
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        ArrayList<Book> list = SearchByPageDao.SearchByPage(start, rows, bookname, auther);
        pageBean.setList(list);
        //计算总页数
        int totalPage = (totalCount % rows) == 0 ? totalCount / rows : (totalCount / rows) + 1;
        pageBean.setTotalPage(totalPage);

        return pageBean;
    }
}
